import {
	request
} from '@/api/request/index.js'
import cartModel from '@/api/cart/model.js'
export default {
	data() {
		return {
			moreicon: this.$resBaseurl + "/act/mrhappy/share.png",
			isMpweixin: false,
			classifyName: '',
			contentId: '',
			main: {

			},
			commodityList: [],
			packageList: [],
			choosepackagename: '',
			showCartBox: false,
			cartpackageList: [],
			cartNum: cartModel.getCartNum(),
			shareConfig: {
				Title: '嗨焙鲜生',
				Contents: '嗨焙鲜生',
				ShareLink: this.$currentlocation.getCurrentPageUrlWithArgs(),
				ImgsrcMp: ''
			}
		}
	},
	onLoad: function(options) {
		let contentId = options.contentid || ''
		let classifyName = options.classifyname || ''
		if (!classifyName) return uni.showToast({
			title: '缺少分类名称',
			icon: 'none'
		})

		this.classifyName = classifyName
		this.contentId = contentId

		if (contentId) this.getItem()
		else
			return uni.showToast({
				title: '缺少分类id',
				icon: 'none'
			})

		// #ifdef H5
		this.isMpweixin = false
		this.initShare()
		// #endif
		// #ifdef MP-WEIXIN
		this.isMpweixin = true
		// #endif
	},
	//微信小程序分享
	onShareAppMessage: function(res) {
		if (res.from === 'button') {}
		return {
			title: this.shareConfig.Title,
			path: this.$currentlocation.getCurrentPageUrlWithArgs(),
			imageUrl: this.shareConfig.ImgsrcMp,
			success(res) {}
		};
	},
	methods: {
		getItem() {
			let that = this;
			uni.showLoading({
				mask: true
			});
			request.get({
					data: {
						c: 'SubjectController',
						m: 'SubjectList',
						classifyName: that.classifyName,
						contentId: that.contentId
					}
				})
				.then(res => {
					console.log(res)
					if (res.Status == "ok") {
						let contents = res.Tag ? res.Tag.contents : [],
							commoditylist = res.Tag ? res.Tag.commoditylist : [],
							packagelist = res.Tag ? res.Tag.packagelist : [];
						commoditylist = commoditylist || []
						packagelist = packagelist || []
						commoditylist.map((citem) => {
							citem.Size = citem.Unit || "组";
							citem.ImageSrc = that.$listImgsrc.replace('{0}', citem.CommodityName);
							citem.Href = "";
							if (citem.Contents && /(<|&lt;)script.*script(>|&gt;)/.test(citem.Contents)) citem.Contents = "";
							citem.UnitPrice = 0;
							citem.DiscountPrice = 0;
							citem.PackageName = ''
							for (let a = 0; a < packagelist.length; a++) {
								let c1 = packagelist[a]
								if (citem.Id == c1.CommodityId) {
									citem.PackageName = c1.PackageName
									citem.UnitPrice = c1.UnitPrice
									citem.DiscountPrice = c1.DiscountPrice ? parseFloat(c1.DiscountPrice.toFixed(2)) : 0;
									citem.Href = "/pages/detail/detail?cid=" + citem.Id + "&pid=" + c1.Id;

									that.choosepackagename = c1.PackageName
									break;
								}
							}
						})

						that.main = contents.length > 0 ? contents[0] : {}
						that.commodityList = commoditylist
						packagelist.map(function(ritem) {
							ritem.Num = 1;
						})
						that.cartpackageList = packagelist

						if (contents.length > 0) {
							that.shareConfig.Title = contents[0].Title
							that.shareConfig.Contents = contents[0].Title
						}
					} else {
						return uni.showToast({
							title: res.Msg,
							icon: 'none'
						})
					}
				})
				.catch(error => {
					console.error(error)

				});
		},
		navigatorTo(href) {
			if (href.indexOf('/pages/tabBar/') > -1) {
				uni.switchTab({
					url: href
				})
			} else
				uni.navigateTo({
					url: href
				})
		},
		openCartBox(item) {
			let that = this;
			let commodityId = item.Id,
				packagename = item.PackageName;
			let sizelist = []
			let resultlist = this.cartpackageList.filter(function(ritem) {
				return ritem.CommodityId == item.Id
			})
			resultlist.map((r) => {
				if (r.DiscountPrice) r.DiscountPrice = parseFloat(r.DiscountPrice.toFixed(2));
				else r.DiscountPrice = 0;
				sizelist.push(r.PackageName);
				r.Num = 1;
				r.PackageNameList = sizelist;
				r.detailsId = r.Id;
				r.Id = item.Id;
				r.CommodityName = item.CommodityName;
				r.Title = item.Title;
				r.DeliveryCity = item.DeliveryCity;
				r.DeliveryStore = item.DeliveryStore;
				r.IsFlash = item.IsFlash;
				r.Size = item.Size;
				r.Remarks = item.Remarks
				r.ImagrUrl = that.$detailsImgsrc.replace('{0}', item.CommodityName);
			})
			this.choosepackagename = item.PackageName;
			this.showCartBox = true;
			this.packageList = resultlist
		},
		chooseCartBox: function(s) {
			this.choosepackagename = s;
		},
		closeCartBox: function() {
			this.showCartBox = false;
		},
		updateCartNum: function(item, type) {
			item.Num = item.Num + type <= 1 ? 1 : item.Num + type;
		},
		addTocart(item) {
			let cartVals = {
				CommodityId: item.Id,
				CommodityName: item.CommodityName,
				CommodityDesc: item.Title || '',
				PackageId: item.detailsId,
				PackageSupplyNo: item.PackageSupplyNo,
				PackageName: item.PackageName,
				Price: item.UnitPrice,
				ZkPrice: item.DiscountPrice,
				DetailsList: [],
				ExpireTime: '',
				DeliveryCity: item.DeliveryCity,
				DeliveryStore: item.DeliveryStore,
				IsFlash: item.IsFlash || false,
				Num: item.Num,
				Size: item.Size
			};

			let r = cartModel.add(cartVals)
			if (r.length > 0) {
				uni.showToast({
					title: '已加入购物车',
					icon: 'none'
				})

				this.cartNum = cartModel.getCartNum()

			}
		},
		initShare() {
			let that =this
			this.$ShareSdk.Share({
				title: that.shareConfig.Title,
				link: that.$currentlocation.getCurrentPageUrlWithArgs(),
				imgUrl:that.$resBaseurl+'/m-images/share/index.jpg',
				success() {
					
				}
			})
		},
	}
}
